<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die;

class AutogeneratorControllerImport extends JControllerForm
{

    public function __construct($config = array())
    {
        parent::__construct($config);
    }
    /**
     * 
     * @param string $name
     * @param string $prefix
     * @param array $config
     * @return AutogeneratorModelImport
     */
    public function getModel($name = '', $prefix = '', $config = array()) {
        return parent::getModel($name, $prefix, $config);
    }

    public function save($sKey = null, $sUrlVar = null)
    {
        // Check for request forgeries.
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $oApplication = JFactory::getApplication();
        $oLanguage = JFactory::getLanguage();

        $oModel = $this->getModel();
        
        $oTable = $oModel->getTable();

        $aData = $this->input->post->get('jform', array(), 'array');

        $iComponentId = isset($aData['component_id']) ? $aData['component_id'] : 0;

        $bCheckIn = property_exists($oTable, 'checked_out');

        $sContext = "$this->option.edit.$this->context";

        $sTask = $this->getTask();

        // Validate the posted data.
        // Sometimes the form needs some posted data, such as for plugins and modules.
        $oForm = $oModel->getForm($aData, false);

        if (!$oForm)
        {
            $oApplication->enqueueMessage($oModel->getError(), 'error');

            return false;
        }

        // Test whether the data is valid.
        $oValidData = $oModel->validate($oForm, $aData);

        // Check for validation errors.
        if ($oValidData === false)
        {
            // Get the validation messages.
            $aErrors = $oModel->getErrors();

            // Push up to three validation messages out to the user.
            for ($i = 0, $n = count($aErrors); $i < $n && $i < 3; $i++)
            {
                if ($aErrors[$i] instanceof Exception)
                {
                    $oApplication->enqueueMessage($aErrors[$i]->getMessage(), 'warning');
                }
                else
                {
                    $oApplication->enqueueMessage($aErrors[$i], 'warning');
                }
            }

            // Save the data in the session.
            $oApplication->setUserState($sContext . '.data', $aData);

            // Redirect back to the edit screen.
            $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=edit&iComponentId=' . $iComponentId, false));

            return false;
        }

        // Attempt to save the data.
        if (!$oModel->save($oValidData))
        {
            // Redirect back to the edit screen.
            $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $oModel->getError()));

            $this->setMessage($this->getError(), 'error');

            $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=edit&iComponentId=' . $iComponentId, false));

            return false;
        }

        $this->setMessage(JText::_(($oLanguage->hasKey($this->text_prefix . ($oApplication->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS') ? $this->text_prefix : 'JLIB_APPLICATION') . ($oApplication->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'));

        // Redirect the user and adjust session state based on the chosen task.
        switch ($sTask)
        {
            case 'apply':
                $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=edit&iComponentId=' . $iComponentId, false));
                break;

            default:
                $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=tables', false));
                break;
        }

        return true;
    }

    public function cancel($key = null)
    {
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $oApplication = JFactory::getApplication();
        
        $sContext = "$this->option.edit.$this->context";

        $oApplication->setUserState($sContext . '.data', null);

        $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=tables', false));

        return true;
    }

}
